package com.itheima.service.mongo;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存动态评论(返回操作之后的次数)
    @Override
    public Integer saveMovementComment(Comment comment) {
        //1. 设置comment属性(缺失publishId commentType userId content)
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(mongoTemplate.findById(comment.getPublishId(), Movement.class).getUserId());

        //向评论表中添加评论
        mongoTemplate.save(comment);

        //判断commentType,进行movement信息更新
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (comment.getCommentType() == 1) {    //点赞
            movement.setLikeCount(movement.getLikeCount() + 1);
        } else if (comment.getCommentType() == 2) { //评论数
            movement.setCommentCount(movement.getCommentCount() + 1);
        } else if (comment.getCommentType() == 3) { //喜欢数
            movement.setLoveCount(movement.getLoveCount() + 1);
        }

        //更新操作
        mongoTemplate.save(movement);

        System.out.println("countLike:"+movement.getLikeCount());
        System.out.println("countLove:"+movement.getLoveCount());

        if (comment.getCommentType() == 1) {    //点赞
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) { //评论数
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) { //喜欢数
            return movement.getLoveCount();
        }

        return 0;
    }


    //删除动态评论(返回操作之后的次数)
    @Override
    public Integer deleteMovementComment(Comment comment) {

        //设置查询条件
        Query query = new Query(Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
        );

        //向评论表中删除一条评论
        mongoTemplate.remove(query, Comment.class);

        //对动态信息进行更新
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() - 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() - 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() - 1);
        }

        //保存更改
        mongoTemplate.save(movement);

        System.out.println("countLike:"+movement.getLikeCount());
        System.out.println("countLove:"+movement.getLoveCount());

        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        }

        return 0;
    }


    //根据被操作人id和操作类型查询
    @Override
    public PageBeanVo findCountComment(Long userId, Integer commentType, Integer pageNum, Integer pageSize) {

        Query query = new Query(Criteria.where("userId").is(userId)
                .and("commentType").is(commentType)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long count = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum,pageSize,count,commentList);
    }


    //动态评论列表
    @Override
    public PageBeanVo findMovementComment(String movementId, Integer pageNum, Integer pageSize) {
        //1. 组装查询条件
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(2)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //2. 统计总记录数
        long count = mongoTemplate.count(query, Comment.class);

        //3. 返回封装结果
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    //保存点赞
    @Override
    public void likeVideo(Comment comment) {
        //1. 设置comment属性(缺失publishId commentType userId content)
        comment.setCreated(System.currentTimeMillis());
        comment.setId(new ObjectId());
        comment.setPublishUserId(mongoTemplate.findById(comment.getPublishId(), Video.class).getUserId());

        //父节点video对象count+1
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));//id为video的主键
        Update update = new Update();
        //查询原数据
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        update.set("likeCount", video.getLikeCount() + 1);
        mongoTemplate.updateFirst(query,update,Video.class);
        //向评论表中添加评论
        mongoTemplate.save(comment);
    }
    //取消点赞
    @Override
    public void dislikeVideo(Comment comment) {
        //父节点video对象count-1
        Query query1 = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        //查询原数据
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        update.set("likeCount", video.getLikeCount() - 1);
        mongoTemplate.updateFirst(query1,update,Video.class);

        //设置查询条件
        Query query = new Query(Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
        );

        //向评论表中删除一条评论
        mongoTemplate.remove(query, Comment.class);
    }

    //保存视频评论
    @Override
    public void saveVideoComment(Comment comment) {
        //将video中的commentCount加1
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        update.set("commentCount", video.getCommentCount() + 1);
        mongoTemplate.updateFirst(query,update,Video.class);


        //1. 设置comment属性(缺失publishId commentType userId content)
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(mongoTemplate.findById(comment.getPublishId(), Video.class).getUserId());
        //向评论表中添加评论
        mongoTemplate.save(comment);

    }
}
